// MainDlg.h : interface of the CMainDlg class
//
/////////////////////////////////////////////////////////////////////////////

#pragma once

#include "ControlEx/ForUIDialog.h"
#include "ControlEx/ControlUI.h"
#include "ControlEx/UIConfig.h"
#include "MainView.h"
//#include "PainterView.h"
#include "ControlEx/DrawableObject.h"
#include "ControlEx/DrawableBackground.h"

#define MAINDLG_WIDTH	800
#define MAINDLG_HEIGHT	600

class CMainDlg : public CForUIDialog
{
public:
	enum { IDD = IDD_MAINDLG };

	//virtual BOOL PreTranslateMessage(MSG* pMsg)
	//{
	//	return CWindow::IsDialogMessage(pMsg);
	//}

	virtual BOOL OnIdle()
	{
		UIUpdateChildWindows();
		return FALSE;
	}

	BEGIN_UPDATE_UI_MAP(CMainDlg)
	END_UPDATE_UI_MAP()

	BEGIN_MSG_MAP(CMainDlg)
		MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
		//MESSAGE_HANDLER(WM_SHOWWINDOW, OnShowMainDlg)
		MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
		//COMMAND_ID_HANDLER(ID_APP_ABOUT, OnAppAbout)
		COMMAND_ID_HANDLER(IDOK, OnOK)
		COMMAND_ID_HANDLER(IDCANCEL, OnCancel)
		CHAIN_MSG_MAP(CForUIDialog)
	END_MSG_MAP()

// Handler prototypes (uncomment arguments if needed):
//	LRESULT MessageHandler(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
//	LRESULT CommandHandler(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
//	LRESULT NotifyHandler(int /*idCtrl*/, LPNMHDR /*pnmh*/, BOOL& /*bHandled*/)

	LRESULT OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		// center the dialog on the screen
		CForUIDialog::OnInitDialog(uMsg, wParam, lParam, bHandled);
		CenterWindow();

		// set icons
		HICON hIcon = AtlLoadIconImage(IDR_MAINFRAME, LR_DEFAULTCOLOR, ::GetSystemMetrics(SM_CXICON), ::GetSystemMetrics(SM_CYICON));
		SetIcon(hIcon, TRUE);
		HICON hIconSmall = AtlLoadIconImage(IDR_MAINFRAME, LR_DEFAULTCOLOR, ::GetSystemMetrics(SM_CXSMICON), ::GetSystemMetrics(SM_CYSMICON));
		SetIcon(hIconSmall, FALSE);

		// register object for message filtering and idle updates
		CMessageLoop* pLoop = _Module.GetMessageLoop();
		ATLASSERT(pLoop != NULL);
		pLoop->AddMessageFilter(this);
		pLoop->AddIdleHandler(this);

		UIAddChildWindowContainer(m_hWnd);


		bool ret = false;
		CUIConfig *pUIconfig = CUIConfig::GetUIConfigInstance();
		if (pUIconfig)
			ret = pUIconfig->ReadJsonConfig(L"\\default\\skin.json");

		if (ret)
		{
			CCtrlBuilder  *pBuilder = this->GetCtrlBuilder();
			CDrawableObjectPtr imgBg = CDrawableObjectPtr(new CDrawableBackground(L"\\resource\\title.png", Color(254, 55,158,223), Color(254, 18,61,98), true, true));
			pBuilder->SetWndLayered(imgBg);
			pBuilder->SetWindowLayer(false);
			SetWindowPos(NULL, 0,0, MAINDLG_WIDTH, MAINDLG_HEIGHT, SWP_NOMOVE|SWP_NOZORDER);
			pBuilder->SetDispSize(MAINDLG_WIDTH, MAINDLG_HEIGHT);

			m_pToolbar = new CMainView;
			pBuilder->Create(m_pToolbar, UIRect(10, 10, 20, 20), L"CMainView");
			//m_pToolbar->ShowWindow(SW_SHOW);
			//m_pPainter = new CPainterView(300, 300);
			//pBuilder->Create(m_pPainter, rcClient, L"CPainterView");
			//m_pPainter->InsertLayer(0, L"D:\\test2dbaidu.bmp");

			pBuilder->RePaint();
		}
		CenterWindow();

		return TRUE;
	}
//LRESULT OnShowMainDlg(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
//{
//	int nCmdShow = wParam;
//	if (nCmdShow == SW_SHOW)
//	{
//        bool from_other = (bool)lParam;
//		//this->ShowWindow(SW_RESTORE);
//		//OnBtnCalendar();
//  //      m_pCalendarView->ShowThisMonthPage();
//        ::SetForegroundWindow(m_hWnd);
//        RECT clock_rect;
//        ::GetWindowRect(m_hWnd, &clock_rect);
//        ::SetWindowPos(m_hWnd, HWND_TOPMOST, clock_rect.left, clock_rect.top, 0, 0,SWP_NOSIZE | SWP_SHOWWINDOW);
//        
//        if (from_other)
//            return 1;
//		POINT lpPoint;
//		GetCursorPos(&lpPoint);
//		SetCursorPos(clock_rect.left + 30, clock_rect.top + 20);
//		//::Sleep(100);
//		mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);
//		mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);
//		SetCursorPos(lpPoint.x, lpPoint.y);
//
//		return 1;
//
//	}else if (nCmdShow == SW_HIDE)
//	{
//		//if (m_settingDlg.IsWindow())
//  //          m_settingDlg.ShowWindow(nCmdShow);
//  //      //if(m_pCalendarView->IsWindowVisible())
//  //          m_pCalendarView->ShowDetailWnd(nCmdShow);
//	}
//	//this->ShowWindow(nCmdShow);
//	return 1;
//}

	LRESULT OnDestroy(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
	{
		// unregister message filtering and idle updates
		CMessageLoop* pLoop = _Module.GetMessageLoop();
		ATLASSERT(pLoop != NULL);
		pLoop->RemoveMessageFilter(this);
		pLoop->RemoveIdleHandler(this);

		return 0;
	}

	//LRESULT OnAppAbout(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	//{
	//	CAboutDlg dlg;
	//	dlg.DoModal();
	//	return 0;
	//}

	LRESULT OnOK(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		// TODO: Add validation code 
		CloseDialog(wID);
		return 0;
	}

	LRESULT OnCancel(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		CloseDialog(wID);
		return 0;
	}

	void CloseDialog(int nVal)
	{
		DestroyWindow();
		::PostQuitMessage(nVal);
	}
private:
	CMainView* m_pToolbar;
	//CPainterView* m_pPainter;
};
